package com.fjxd.plane.web;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.fjxd.plane.pojo.Airlines;
import com.fjxd.plane.pojo.Plane;
import com.fjxd.plane.service.AirlinesService;
import com.fjxd.plane.service.PlaneService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import com.fjxd.feign.result.ViewResult;
@CrossOrigin
@RestController
@RequestMapping("/plane")
public class PlaneController {
    @Autowired
    PlaneService planeService;
    @Autowired
    AirlinesService airlinesService;
    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping
    public List<Plane> getPlaneAll() {
        return planeService.getPlaneAll();
    }

    @GetMapping("/{id}")
    public Plane getPlaneById(@PathVariable Integer id) {
        String key = "plane:" + id;
        String plane = redisTemplate.opsForValue().get(key);
        if (StrUtil.isNotBlank(plane)) {
            return JSONUtil.toBean(plane, Plane.class);
        } else {
            Plane planeById = planeService.getPlaneById(id);
            redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(planeById));
            return planeById;
        }
    }

    @GetMapping("/getAirlines")
    public List<Airlines>  getAirlines(){
        return airlinesService.getAirlinesAll();
    }

    @GetMapping("/getAirlines/{id}")
    public Airlines getAirlinesByName(@PathVariable Integer id){
        return airlinesService.getAirlinesByName(id);
    }



    @PostMapping()
    public ViewResult addPlane(@RequestBody Plane plane){
        boolean flag = planeService.save(plane);
        return new ViewResult(ViewResult.Status.SUCCESS.getCode(),"新增成功！");
    }

    @PutMapping()
    public ViewResult updatePlane(@RequestBody Plane plane){
        boolean flag = planeService.updateById(plane);
        return new ViewResult(ViewResult.Status.SUCCESS.getCode(),"修改成功！");
    }

    @DeleteMapping("/{id}")
    public ViewResult deletePlane(@PathVariable String  id){
        boolean flag = planeService.removeById(id);
        return new ViewResult(ViewResult.Status.SUCCESS.getCode(),flag? "删除成功！" : "删除失败！");
    }

    @PostMapping("/addAll")
    public ViewResult addAllPlane() throws ParseException {
        DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        List<Airlines> airlinesList = airlinesService.list();
        List<Plane> planeList = new ArrayList<Plane>();
        planeList.add(new Plane("-C919-00","C919",190,0,36,154,420));
        planeList.add(new Plane("-146-00","BAE-146",108,0,22,86,680));
        planeList.add(new Plane("-310-00","空中客车310",250,20,50,180,450));
        planeList.add(new Plane("-747-00","波音747",350,30,70,250,400));
        planeList.add(new Plane("-YN7-00","运7",50,0,10,40,700));
        int num = 1;//用于飞机编号累加
        String sum = "";//用于将飞机编号转换为字符串
        for (int j = 0; j < 95; j++) {
            for (int k = 0; k < planeList.size(); k++){
                for (int i = 0; i < airlinesList.size(); i++) {
                    sum = num < 10 ? ("0" + num) : ("" + num);
                    Plane plane = planeList.get(k);
                    Plane plane1 = new Plane(airlinesList.get(i).getLetter()+plane.getSerial()+sum,airlinesList.get(i).getId(),plane.getName(),
                            plane.getTakeNumber(),plane.getFirstClass(),plane.getBusinessClass(),plane.getEconomyClass(),airlinesList.get(i),plane.getSpeed());
                    planeService.addPlane(plane1);
                }
            }
            num++;
        }

        return new ViewResult(ViewResult.Status.SUCCESS.getCode(),"新增成功！");
    }
}
